#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void grep(FILE *fp, char *const argv1)
{

  if (fp == NULL)
  {
    fprintf(stdout, "cannot open file\n");
    exit(EXIT_FAILURE);
  }

  char *line = NULL;
  size_t len = 0;
  while (getline(&line, &len, fp) != -1)
  {
    if (strstr(line, argv1) != NULL)
    {
      printf("%s", line);
    }
  }
}

int main(int argc, char *const argv[])
{
  if (argc == 1)
  {
    fprintf(stdout, "searchterm [file ...]\n");
    exit(EXIT_FAILURE);
  }
  if (argc == 2)
  {
    grep(stdin, argv[1]);
  }
  for (size_t i = 2; i < argc; ++i)
  {
    FILE *fp = fopen(argv[i], "r");
    grep(fp, argv[1]);
  }
}